/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.convertor;

import info.peigen.hotpot.manage.core.domain.User;
import info.peigen.hotpot.manage.core.manage.form.UserCreateForm;
import info.peigen.hotpot.manage.core.manage.form.UserEditForm;
import info.peigen.hotpot.manage.core.manage.form.UserSearchForm;
import info.peigen.hotpot.manage.core.repository.entity.UserEntity;
import info.peigen.hotpot.manage.core.service.order.UserCreateOrder;
import info.peigen.hotpot.manage.core.service.order.UserEditOrder;
import info.peigen.hotpot.manage.core.service.result.info.UserInfo;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * <b>(UserManageConvertor)</b>
 * 管理后台领域对象 "用户" 转换器
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Mapper(builder = @Builder(disableBuilder = true))
public interface UserManageConvertor {
    UserManageConvertor INSTANCE = Mappers.getMapper(UserManageConvertor.class);

    // ConvertToEntity
    @Mappings({
            @Mapping(target = "roleIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(domain.getRoles())?\"\":domain.getRoles().stream().map(d -> String.valueOf(d.getRoleId())).collect(java.util.stream.Collectors.joining(\",\")))"),
            @Mapping(target = "positionIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(domain.getPositions())?\"\":domain.getPositions().stream().map(d -> String.valueOf(d.getPositionId())).collect(java.util.stream.Collectors.joining(\",\")))"),
            @Mapping(target = "organizationIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(domain.getOrganizations())?\"\":domain.getOrganizations().stream().map(d -> String.valueOf(d.getOrganizationId())).collect(java.util.stream.Collectors.joining(\",\")))"),
    })
    UserEntity convertToEntity(User domain);

    List<UserEntity> convertToEntitiesByDomain(List<User> domains);

    @Mappings({
            @Mapping(target = "roleIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(userInfo.getRoleInfos())?\"\":userInfo.getRoleInfos().stream().map(i -> String.valueOf(i.getRoleId())).collect(java.util.stream.Collectors.joining(\",\")))"),
            @Mapping(target = "positionIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(userInfo.getPositionInfos())?\"\":userInfo.getPositionInfos().stream().map(i -> String.valueOf(i.getPositionId())).collect(java.util.stream.Collectors.joining(\",\")))"),
            @Mapping(target = "organizationIds", expression = "java(cn.hutool.core.util.ObjUtil.isEmpty(userInfo.getOrganizationInfos())?\"\":userInfo.getOrganizationInfos().stream().map(i -> String.valueOf(i.getOrganizationId())).collect(java.util.stream.Collectors.joining(\",\")))"),
    })
    UserEntity convertToEntity(UserInfo userInfo);

    List<UserEntity> convertToEntitiesByInfo(List<UserInfo> userInfo);

    UserEntity convertToEntity(UserSearchForm form);

    UserEntity convertToEntity(UserCreateForm form);

    UserEntity convertToEntity(UserEditForm form);

    // ConvertToDomain
    @Mappings({
            @Mapping(target = "roles", ignore = true),
            @Mapping(target = "positions", ignore = true),
            @Mapping(target = "organizations", ignore = true),
    })
    User convertToDomain(UserEntity entity);

    List<User> convertToDomainsByEntity(List<UserEntity> entities);

    @Mappings({
            @Mapping(target = "roles", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.RoleManageConvertor.INSTANCE.convertToDomainsByInfo(userInfo.getRoleInfos()))"),
            @Mapping(target = "positions", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.PositionManageConvertor.INSTANCE.convertToDomainsByInfo(userInfo.getPositionInfos()))"),
            @Mapping(target = "organizations", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.OrganizationManageConvertor.INSTANCE.convertToDomainsByInfo(userInfo.getOrganizationInfos()))"),
    })
    User convertToDomain(UserInfo userInfo);

    List<User> convertToDomainsByInfo(List<UserInfo> infos);

    User convertToDomain(UserSearchForm form);

    @Mappings({
            @Mapping(target = "roles", ignore = true),
            @Mapping(target = "positions", ignore = true),
            @Mapping(target = "organizations", ignore = true),
    })
    User convertToDomain(UserCreateForm form);

    @Mappings({
            @Mapping(target = "roles", ignore = true),
            @Mapping(target = "positions", ignore = true),
            @Mapping(target = "organizations", ignore = true),
    })
    User convertToDomain(UserEditForm form);

    // ConvertToInfo
    @Mappings({
            @Mapping(target = "roleInfos", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.RoleManageConvertor.INSTANCE.convertToInfosByDomain(domain.getRoles()))"),
            @Mapping(target = "positionInfos", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.PositionManageConvertor.INSTANCE.convertToInfosByDomain(domain.getPositions()))"),
            @Mapping(target = "organizationInfos", expression = "java(info.peigen.hotpot.manage.core.manage.convertor.OrganizationManageConvertor.INSTANCE.convertToInfosByDomain(domain.getOrganizations()))"),
    })
    UserInfo convertToInfo(User domain);

    List<UserInfo> convertToInfosByDomain(List<User> domains);

    @Mappings({
            @Mapping(target = "roleInfos", ignore = true),
            @Mapping(target = "positionInfos", ignore = true),
            @Mapping(target = "organizationInfos", ignore = true),
    })
    UserInfo convertToInfo(UserEntity entity);

    List<UserInfo> convertToInfosByEntity(List<UserEntity> entities);

    UserInfo convertToInfo(UserSearchForm form);

    @Mappings({
            @Mapping(target = "roleInfos", ignore = true),
            @Mapping(target = "positionInfos", ignore = true),
            @Mapping(target = "organizationInfos", ignore = true),
    })
    UserInfo convertToInfo(UserCreateForm form);

    @Mappings({
            @Mapping(target = "roleInfos", ignore = true),
            @Mapping(target = "positionInfos", ignore = true),
            @Mapping(target = "organizationInfos", ignore = true),
    })
    UserInfo convertToInfo(UserEditForm form);

    // ConvertOrder
    UserCreateOrder convertToCreateOrder(UserCreateForm form);

    UserEditOrder convertToEditOrder(UserEditForm form);
}